<?

	class InlineImage
	{
		var $idImagen;
		var $contentType;
		
		function InlineImage($idImagen, $contentType)
		{
			$this->idImagen = $idImagen;
			$this->contentType = $contentType;
		}
	}
	

class SendMail
{
	var $to;
	var $cc;
	var $bcc;
	var $fromEmail;
	var $fromName;
	var $replyTo;
	var $subject;
	var $exito;
	var $html;
	var $contentType;	
	var $messageBoundary;
	var $attachments;
	var $imagenes;
	var $cantImagenes;

// El content type multipart/alternative muestra la misma info en distintos formatos
// ver de implementar para programas de email que no se banquen HTML
// Ejemplo
/*
	From: Nathaniel Borenstein <nsb@bellcore.com>
	To: Ned Freed <ned@innosoft.com>
	Date: Mon, 22 Mar 1993 09:41:09 -0800 (PST)
	Subject: Formatted text mail
	MIME-Version: 1.0
	Content-Type: multipart/alternative; boundary=boundary42
	
	--boundary42
	Content-Type: text/plain; charset=us-ascii
	
		... plain text version of message goes here ...
		
	--boundary42
	Content-Type: text/enriched
	
		... RFC 1896 text/enriched version of same message goes here ...
		
	--boundary42
	Content-Type: application/x-whatever
	
		... fanciest version of same message goes here ...
		
	--boundary42--
*/
	function SendMail()
	{
		$this->contentType = 'multipart/mixed';
		$this->messageBoundary = 'message-boundary-gc0pJq0M:08jU534c0p-gc0pJq0M:08jU534c0p';
		$this->attachments = array();
		$this->imagenes = array();
		$this->cantImagenes = 0;
	}
	
	// metodos publicos

	function addAttachment($file, $contentType)
	{
		//$contentType = mime_content_type($file);
		
		$this->attachments[$file] = $contentType;
	}
	
	function addInlineImage($file, $contentType)
	{
		//$contentType = mime_content_type($file);
		
		$idImagen = 'imagen' . $this->cantImagenes;
		$inImg = new InlineImage($idImagen, $contentType);
		$this->imagenes[$file] = $inImg;
		$this->cantImagenes++;
	}

// charset=iso-8859-1
// charset=iso-8859-6
// charset=windows-1252
// text/html; charset=iso-8859-1
	function setHTML($html)
	{
		$this->html = $this->getMessageBoundary();
		$this->html .= "Content-type: text/html; charset=iso-8859-1\r\n";
		$this->html .= "Content-transfer-encoding: 7BIT\r\n";
		$this->html .= "Content-description: Mail message body\r\n\r\n";
		$this->html .= $html;
		$this->html .= "\r\n";
	}
	
	function setSubject($subject)
	{
		$this->subject = $subject;
	}
	
	function setTo($to)
	{
		$this->to = $to;
	}
	
	function setCC($cc)
	{
		$this->cc = $cc;
	}
	
	function setBCC($bcc)
	{
		$this->bcc = $bcc;
	}
	
	function setFrom($email, $name = null)
	{
		$this->fromEmail = $email;
		if($name != null)
			$this->fromName = $name;
	}
	
	function setReplyTo($replyTo)
	{
		$this->replyTo = $replyTo;
	}

	function enviar()
	{
		$this->exito = mail($this->getTo(), $this->getSubject(), $this->getBody(), $this->getHeaders());
		return $this->exito;
	}	
	
	function getInlineImageSrc($file)
	{
		return 'cid:' . $this->imagenes[$file]->idImagen;
	}

	
	// metodos privados

	function getSubject()
	{
		return $this->subject;
	}
	
	function getTo()
	{
		return $this->to;
	}

	function getHeaders()
	{
		$headers = '';
		if($this->fromName && $this->fromEmail)
		{
			$headers .= "From: ". $this->fromName ." <". $this->fromEmail .">\r\n";
		}
		else
		{
			if($this->fromEmail)
				$headers .= "From: ". $this->fromEmail .">\r\n";
		}
		
		if($this->cc)
			$headers .= "Cc: ". $this->cc ."\r\n";
		
		if($this->bcc)
			$headers .= "Bcc: ". $this->bcc ."\r\n";
		
		if($this->replyTo)
			$headers .= "Reply-To: ". $this->replyTo ."\r\n";
		
		$headers .= "MIME-version: 1.0\r\n";
		$headers .= 'Content-type: ' . $this->contentType . '; boundary="'. $this->messageBoundary . '"' . "\r\n" ;
		$headers .= "Content-transfer-encoding: 7BIT\r\n";
		
		return $headers;
	}

	
	function getMessageBoundary()
	{
		return "\r\n--" . $this->messageBoundary . "\r\n";
	}
	
	function getEndOfMessage()
	{
		return "\r\n--" . $this->messageBoundary . "--\r\n";
	}
	
	function getHTML()
	{
		return $this->html;
	}
	
	function getBody()
	{
		// el orden de las cosas importa
		// primero va el HTML luego las imagenes y adjuntos
		// siempre termina con un boundary con -- agregado al final
		$body = $this->getHTML();
		$body .= $this->getInlineImages();
		$body .= $this->getAttachments();
		$body .= $this->getEndOfMessage();
		
		return $body;
	}


/*
         Content-Type: image/jpeg
         Content-Disposition: inline
         Content-Description: just a small picture of me

         <jpeg data>
*/	
	function getInlineImages()
	{
		$images = '';
		foreach($this->imagenes as $file => $inImg)
		{
			$fileName = basename($file);
			// file_get_contents is binary-safe
			$contents = file_get_contents($file, filesize($file));
			$encoded_attach = chunk_split(base64_encode($contents));
							
			$images .= $this->getMessageBoundary();
			$images .= 'Content-type: '. $inImg->contentType .'; name="'. $fileName .'"'. "\r\n";
//			$images .= 'Content-Type: '. $inImg->contentType . "\r\n";
			$images .= "Content-Disposition: inline\r\n";
			$images .= "Content-Transfer-Encoding: BASE64\r\n";
			$images .= "Content-ID: <". $inImg->idImagen .">\r\n\r\n";
			$images .= "$encoded_attach\r\n";
		}
		
		return $images;
	}
	

/*
         Content-Type: image/jpeg
         Content-Disposition: attachment; filename=genome.jpeg
         Content-Description: a complete map of the human genome

         <jpeg data>	
*/
	function getAttachments()
	{
		$attachments = '';
		
		foreach($this->attachments as $file => $contentType)
		{
			$fileName = basename($file);
			// file_get_contents is binary-safe
			$contents = file_get_contents($file, filesize($file));
			$encoded_attach = chunk_split(base64_encode($contents));
					
			$attachments .= $this->getMessageBoundary();
			$attachments .= 'Content-type: '. $contentType .'; name="'. $fileName .'"'. "\r\n";
			$attachments .= "Content-transfer-encoding: 7BIT\n";
			$attachments .= 'Content-disposition: attachment; filename="'. $fileName .'"'. "\r\n\r\n";
			$attachments .= "$encoded_attach\r\n";
		}
		
		return $attachments;
	}
	
	
	
	
}
?>